<?php
/* ----------------------------------------------------------
--               JacoCMS by Jaco Ruit                      --
-------------------------------------------------------------
--     JacoCMS is Open Source and is lincenced under       --
--             GNU General Public Licence v3               --
--          http://www.gnu.org/copyleft/gpl.html           --
-------------------------------------------------------------
--       Please don't remove any text from the footers.    --
-------------------------------------------------------------
-- Credits:   * Jaco Ruit          ~     Scripts,MySQL     --     
-----------------------------------------------------------*/

# JacoCMS Article Class

class Article
{

	
	public function getByID ($idtosearch)
	{
		$query = "SELECT content, title, date, summary, author, keywords FROM articles WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $idtosearch;
			$date = $row['date'];
			$title = $row ['title'];
			$summary = $row['summary'];
			$content = $row['content'];
			$writer = $row['author'];
			$keywords = $row['keywords'];
		}
		$arr = array("id" => $id, "date" => $date, "author" => $writer, "title" => $title, "summary" => $summary, "content" => $content, "keywords" => $keywords);
		mysql_free_result($result);
		return $arr;
	}
	
	public function getLastID()
	{
		$query = "SELECT id FROM articles ORDER BY id DESC";
		$result = mysql_query($query);
		$lastid = null;
		$lastid = mysql_fetch_array($result);
		mysql_free_result($result);
		return $lastid['id'];
	}
	
	public function checkIfExists($idtosearch)
	{
		$query = "SELECT title FROM articles WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		if (mysql_num_rows($result) == 1)
		{
			mysql_free_result($result);
			return true;
		}
		else
		{
			mysql_free_result($result);
			return false;
		}
	}
	
	public function rowCount()
	{
		$query = "SELECT title FROM articles";
		$result = mysql_query($query);
		$count = mysql_num_rows($result);
		return $count;
	}
	
	public function update ($idtoup, $title, $summary, $content, $keywords)
	{
		$query = "UPDATE articles SET title = '" . $title . "', summary = '" . $summary . "', content = '" . $content . "', keywords =  '" . $keywords . "' WHERE id='" . $idtoup . "'";
		$result = mysql_query($query);
	}
	
	public function add ($date, $author, $title, $summary, $content, $keywords)
	{
		$newid = $this->getLastID() + 1;
		$query = "INSERT INTO articles (id,date,author,title,summary,content,keywords) VALUES ('" . $newid . "','" . $date . "','" . $author . "','" . $title . "','" . $summary . "','" . $content . "','" . $keywords . "')";
		$result = mysql_query($query);
	}
	
	public function delete($idtodel)
	{
		$query = "DELETE FROM articles WHERE id='" . $idtodel . "'";
		$result = mysql_query($query);
	}
	
	public function getTable($array)
	{
		return '<tr><td>' . $array['id'] . '</td><td>' . $array['author'] . '</td><td>' . $array['title'] . '</td><td><a href="deletea-' . $array['id'] . '">Delete</a> <a href="editarticle-' . $array['id'] . '">Edit</a></td></tr>';
	}
	
	
	
	public function getWholeTable($from, $till)
	{
		$htmlcode = "<table><tr><th>ID</th><th>Author</th><th>Title</th></tr>";
		if ($till > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		if ($from > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		$query = "SELECT title,author, id FROM articles ORDER BY id DESC LIMIT " . $from .", " . $till;
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$title = $row ['title'];
			$writer = $row['author'];
			$arr = array("id" => $id, "author" => $writer, "title" => $title);
			$htmlcode .= $this->getTable($arr);
		}
		$htmlcode .= '</table>';
		mysql_free_result($result);
		return $htmlcode;
	}
	
	public function getHTMLCode($array)
	{
		return'<div id="article">
					<h3>' . $array['title'] . '</h3>
					<br />
					<div id="center"><p><b>' . $array['summary'] . '</b></p></div>
					<br />
			<div id="articlefooter">
			<p>Posted on ' . $array['date'] . ' by ' . $array['author'] . ' ~ Read <a href="article-' . $array['id'] . '"> more..</a></p>
			</div></div>';
	}
	
	public function getHTMLCode2($array)
	{
		return'<div id="article">
					<h3>' . $array['title'] . '</h3>
					<br />
					<div id="center"><p><b>' . $array['summary'] . '</b></p></div>
					<br />
					<p>' . $array['content'] . '</p>
					<br />
				
					
						<!-- AddThis Button BEGIN -->
					<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
						<a class="addthis_button_preferred_1"></a>
						<a class="addthis_button_preferred_2"></a>
						<a class="addthis_button_preferred_3"></a>
						<a class="addthis_button_preferred_4"></a>
						<a class="addthis_button_compact"></a>
						<a class="addthis_counter addthis_bubble_style"></a>
					</div>
					<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e2f390741f02fb9"></script>
					<!-- AddThis Button END --><div id="articlefooter">
						<p>Posted on ' . $array['date'] . ' by ' . $array['author'] . ' ~ Go <a href="/">back</a></p>
					</div>
				</div>';
	}
	
	public function getMetaTags($array)
	{
		return '<meta name="author" content="' . $array['author'] . '" />
				<meta name="description" content="' . $array['summary'] . '" />
				<meta name="keywords" content="' . $array['keywords'] . '" />
				<meta name="generator" content="JacoCMS" />';
	}
	
	public function getHTMLArchiveCode($from, $till)
	{
		$htmlcode = "";
		if ($till > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		if ($from > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		$query = "SELECT content, title, date, summary, author, keywords, id FROM articles ORDER BY id DESC LIMIT " . $from .", " . $till;
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$date = $row['date'];
			$title = $row ['title'];
			$summary = $row['summary'];
			$content = $row['content'];
			$writer = $row['author'];
			$keywords = $row['keywords'];
			
			$arr = array("id" => $id, "date" => $date, "author" => $writer, "title" => $title, "summary" => $summary, "content" => $content, "keywords" => $keywords);
			$htmlcode .= $this->getHTMLCode($arr);
		}
		
		mysql_free_result($result);
		return $htmlcode;
	}
	
}
?>